package io.gbznt.modules.buss.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.gbznt.modules.buss.entity.BussRtuEquipmentEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.gbznt.common.utils.PageUtils;
import io.gbznt.common.utils.R;
import io.gbznt.common.utils.ShiroUtils;
import io.gbznt.common.validator.ValidatorUtils;
import io.gbznt.common.validator.group.AddGroup;
import io.gbznt.modules.buss.entity.BussFarmlandInfoEntity;
import io.gbznt.modules.buss.entity.BussRtuInfoEntity;
import io.gbznt.modules.buss.service.BussFarmlandInfoService;
import io.gbznt.modules.buss.service.BussRtuInfoService;



/**
 * RTU设备信息
 *
 * @author gbznt
 * @email http://www.gbznt.com/
 * @date 2019-04-20 15:48:33
 */
@RestController
@RequestMapping("buss/bussrtuinfo")
public class BussRtuInfoController {

    @Autowired
    private BussRtuInfoService bussRtuInfoService;
    @Autowired
    private BussFarmlandInfoService bussFarmlandInfoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("buss:bussrtuinfo:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = bussRtuInfoService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("buss:bussrtuinfo:info")
    public R info(@PathVariable("id") String id){
        BussRtuInfoEntity bussRtuInfo = bussRtuInfoService.getById(id);

        // 填充田块名称
        BussFarmlandInfoEntity bussFarmlandInfoEntity = bussFarmlandInfoService.getById(
                bussRtuInfo.getFarmlandInfoId());
        if(null != bussFarmlandInfoEntity) {
            bussRtuInfo.setFarmlandName(bussFarmlandInfoEntity.getName());
        }

        return R.ok().put("bussRtuInfo", bussRtuInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("buss:bussrtuinfo:save")
    public R save(@RequestBody BussRtuInfoEntity bussRtuInfo){
    	ValidatorUtils.validateEntity(bussRtuInfo,AddGroup.class);

        Integer namesNum = bussRtuInfoService.count(
                new QueryWrapper<BussRtuInfoEntity>().eq("serial_num",bussRtuInfo.getSerialNum()));
        if(namesNum >= 1){
            return R.error("该RTU名称已存在");
        }

   	 	bussRtuInfo.setCreateId(ShiroUtils.getUserId()+"");
   	 	bussRtuInfo.setCreateName(ShiroUtils.getUserName());
    	bussRtuInfo.setCreateTime(new Date());
        bussRtuInfoService.save(bussRtuInfo);

        BussFarmlandInfoEntity bussFarmlandInfoEntity = bussFarmlandInfoService.getById(
                bussRtuInfo.getFarmlandInfoId());
        if(null != bussFarmlandInfoEntity) {
            bussRtuInfo.setDeptId(bussFarmlandInfoEntity.getDeptId());
        }

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("buss:bussrtuinfo:update")
    public R update(@RequestBody BussRtuInfoEntity bussRtuInfo){
        ValidatorUtils.validateEntity(bussRtuInfo,AddGroup.class);

        Integer namesNum = bussRtuInfoService.count(
                new QueryWrapper<BussRtuInfoEntity>().eq("serial_num",bussRtuInfo.getSerialNum())
                .ne("id",bussRtuInfo.getId())
        );
        if(namesNum >= 1){
            return R.error("该RTU名称已存在");
        }

        bussRtuInfo.setUpdateId(ShiroUtils.getUserId()+"");
        bussRtuInfo.setUpdateName(ShiroUtils.getUserName());
    	bussRtuInfo.setUpdateTime(new Date());

        BussFarmlandInfoEntity bussFarmlandInfoEntity = bussFarmlandInfoService.getById(
                bussRtuInfo.getFarmlandInfoId());
        if(null != bussFarmlandInfoEntity) {
            bussRtuInfo.setDeptId(bussFarmlandInfoEntity.getDeptId());
        }

        Boolean re = bussRtuInfoService.updateById(bussRtuInfo);//全部更新
        
        return re?R.ok():R.oldVersion();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("buss:bussrtuinfo:delete")
    public R delete(@RequestBody String[] ids){
        bussRtuInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
